from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By

with open('./url.txt', 'r', encoding='utf-8') as f:
    BASE_URL = f.readline()
service = Service(executable_path=r"/home/b8313/Desktop/chromedriver")
driver = webdriver.Chrome(service=service)
driver.get(BASE_URL)
driver.find_element(By.XPATH, '/html/body/header/div[1]/div[1]/div[1]/img').click()
driver.find_element(By.XPATH, '/html/body/div[1]/span').click()
driver.find_element(By.XPATH, '/html/body/div[2]/form/input').send_keys('研究生')
driver.find_element(By.XPATH, '/html/body/div[2]/form/button').click()

driver.implicitly_wait(10)
# print(driver.window_handles)
driver.switch_to.window(driver.window_handles[1])

item1 = driver.find_element(By.XPATH, '/html/body/div[1]/div[5]/div[1]/div/div[1]/h3/a')
print(item1.text)

total_number_span = driver.find_element(By.XPATH, '/html/body/div[1]/div[5]/div[2]/span')
print(total_number_span.text)

list_items_xpath = '/html/body/div[1]/div[5]/div[1]/div/div'
list_items = driver.find_elements(By.XPATH, list_items_xpath)
# //*[@id="search_body"]/div[1]/div/div[1]/span[2]
# //*[@id="search_body"]/div[1]/div/div[1]/h3/a
title_time_list = [
    (i.find_element(By.XPATH, 'span[2]'), i.find_element(By.XPATH, 'h3/a')) for i in list_items
]

# for title_time in title_time_list:
#     print("{} {}".format(title_time[0].text, title_time[1].text))

first_title_time = title_time_list[0]
print("{} {}".format(first_title_time[0].text, first_title_time[1].text))


driver.implicitly_wait(5)
driver.quit()
